<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //1. 数字求和
        function sum(n) {
            if(n<=1) return n
            return sum(n-1) + n 
        }
        // console.log(sum(10))
        // 2.数组求和
        function sumArr(arr) {
            if(arr.length === 1) return arr[0]
            if(!arr.length) return 0
            return arr[0] + sumArr(arr.slice(1))
        }
        let arr = [ 1, 2, 3, 4, 5 ]
        // console.log(sumArr(arr))
        // 3.数组转树
        let arrData = [
            { key: 1, title: '1', pid: 0, isLeaf: false, children: [] },
            { key: 11, title: '1-1', pid: 1, isLeaf: false, children: [] },
            { key: 111, title: '1-1-1', pid: 11, isLeaf: true, children: [] },
            { key: 112, title: '1-1-2', pid: 11, isLeaf: false, children: [] },
            { key: 1121, title: '1-1-2-1', pid: 112, isLeaf: true, children: [] },
            { key: 12, title: '1-2', pid: 1, isLeaf: false, children: [] },

            { key: 2, title: '2', pid: 0, isLeaf: false, children: [] },
            { key: 21, title: '2-1', pid: 2, isLeaf: true, children: [] },
            { key: 22, title: '2-2', pid: 2, isLeaf: false, children: [] },
            { key: 221, title: '2-2-1', pid: 22, isLeaf: true, children: [] },

            { key: 3, title: '3', pid: 0, isLeaf: false, children: [] },
            { key: 31, title: '3-1', pid: 3, isLeaf: true, children: [] },
            { key: 32, title: '3-2', pid: 3, isLeaf: true, children: [] }
        ]
        function toTree(arr, parentId = 0) {
            const children = []
            arr.forEach(item => {
                if(item.pid === parentId){
                    children.push(item)
                    item.children = toTree(arr, item.key)
                }
            });
            return children
        }
        console.log(toTree(arrData, 0))

        const tree = [
            {
                "key": 1,
                "title": "1",
                "pid": 0,
                "isLeaf": false,
                "children": [
                    {
                        "key": 11,
                        "title": "1-1",
                        "pid": 1,
                        "isLeaf": false,
                        "children": [
                            {
                                "key": 111,
                                "title": "1-1-1",
                                "pid": 11,
                                "isLeaf": true,
                                "children": []
                            },
                            {
                                "key": 112,
                                "title": "1-1-2",
                                "pid": 11,
                                "isLeaf": false,
                                "children": [
                                    {
                                        "key": 1121,
                                        "title": "1-1-2-1",
                                        "pid": 112,
                                        "isLeaf": true,
                                        "children": []
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "key": 12,
                        "title": "1-2",
                        "pid": 1,
                        "isLeaf": false,
                        "children": []
                    }
                ]
            },
            {
                "key": 2,
                "title": "2",
                "pid": 0,
                "isLeaf": false,
                "children": [
                    {
                        "key": 21,
                        "title": "2-1",
                        "pid": 2,
                        "isLeaf": true,
                        "children": []
                    },
                    {
                        "key": 22,
                        "title": "2-2",
                        "pid": 2,
                        "isLeaf": false,
                        "children": [
                            {
                                "key": 221,
                                "title": "2-2-1",
                                "pid": 22,
                                "isLeaf": true,
                                "children": [
                                    {
                                        "key": 2211,
                                        "title": "2-2-1-1",
                                        "pid": 1,
                                        "isLeaf": false,
                                        "children": [
                                            {
                                                "key": 22111,
                                                "title": "2-2-1-1-1",
                                                "pid": 11,
                                                "isLeaf": true,
                                                "children": []
                                            },
                                            {
                                                "key": 221111,
                                                "title": "2-2-1-1-2",
                                                "pid": 11,
                                                "isLeaf": false,
                                                "children": [
                                                    {
                                                        "key": 2211111,
                                                        "title": "2-2-1-1-2-1",
                                                        "pid": 112,
                                                        "isLeaf": true,
                                                        "children": []
                                                    }
                                                ]
                                            }
                                        ]
                                    },
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "key": 3,
                "title": "3",
                "pid": 0,
                "isLeaf": false,
                "children": [
                    {
                        "key": 31,
                        "title": "3-1",
                        "pid": 3,
                        "isLeaf": true,
                        "children": []
                    },
                    {
                        "key": 32,
                        "title": "3-2",
                        "pid": 3,
                        "isLeaf": true,
                        "children": []
                    }
                ]
            }
        ]
        const deeparr = []
        function getTreeDeepth(children, deep, deeparr) {
            if(children.length >= 1) deep++
            deeparr.push(deep)
            children.forEach((item)=> {
                getTreeDeepth(item.children, deep, deeparr)
            })
            return deeparr
        }
        console.log(getTreeDeepth(tree, 0 , deeparr))
    </script>
</body>
</html>